

import { Body, Controller, Get,Post, Query } from '@nestjs/common';
import { LogisticsorderService } from 'src/services/logisticsorder/logisticsorder.service';
import { ILogisticsOrder } from 'src/types/logisticsorder.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = ILogisticsOrder[] | ILogisticsOrder;

@Controller('logisticsorder')
export class LogisticsorderController {


 //等价于上面的写法
      constructor(private readonly  logisticsOrderService:LogisticsorderService){

      }

      @Get('/findAll')  
      async findAll():Promise<HttpResponse<ILogisticsOrder[]>>{
         const data = await this.logisticsOrderService.findService();
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/logisticsorder/importData
     */
    @Post('/importData')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async create(@Body() requestParamsBody:UnionType) {

        const data:UnionType = await this.logisticsOrderService.importData(requestParamsBody);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }

      @Get('/getReceiverOrder')  
      async getReceiverOrder(
        @Query('status') status:number=1,
        @Query('page') page:number=1, //查询的页码
        @Query('pageSize') pageSize:number=5  //每页展示的条数
      ):Promise<HttpResponse<ILogisticsOrder[]>>{
        console.log('传递过来的参数',Date.now(),'page=',page,'status=',status,'pageSize=',pageSize)
         const data = await this.logisticsOrderService.fetchLogisticsOrderByStatus(page,pageSize,status);
        
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }


}
